package com.vv.tools.common.demo;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;

/**
 * 线下补单
 */
public class CarroDemo {

    public static void main(String[] args) {
        // 处理线下单
        CarroDemo cd = new CarroDemo();
        cd.run();
    }


    private void run() {
        String applicationId = "979811586479261";
        String policyNo = "MV8832689";
        String insurerBasicPremium = "1516.80";
        String premiumDiscountPercent = "30";
        String premiumDiscount = "455.04";
        String netPremium = "1061.76";
        String premiumTax = "84.94";
        String effectiveDate = "2025-08-01 00:00:00";
        String expiryDate = "2026-07-31 23:59:59";
        String carroDiscount = "0";


        String stampDuty = "10";
        String premiumAfterDiscount = new BigDecimal(netPremium)
            .add(new BigDecimal(premiumTax))
            .add(new BigDecimal(stampDuty))
            .subtract(new BigDecimal(carroDiscount))
            .toPlainString();


        System.out.println(
            updateApplication(applicationId, premiumAfterDiscount, netPremium, policyNo, insurerBasicPremium, premiumDiscount, premiumDiscountPercent,
                carroDiscount, effectiveDate, expiryDate)
        );

        System.out.println();

        System.out.println(
            updateTax(applicationId, premiumTax, netPremium, carroDiscount)
        );
    }

    private String updateApplication(String applicationId,
                                     String premiumAfterDiscount,
                                     String netPremium,
                                     String policyNo,
                                     String insurerBasicPremium,
                                     String premiumDiscount,
                                     String premiumDiscountPercent,
                                     String carroDiscount,
                                     String effectiveDate,
                                     String expiryDate) {
        StringBuilder sb = new StringBuilder();
        sb.append("update carro_trade_00.application set\n");
        if (StringUtils.isNotBlank(premiumAfterDiscount)) {
            sb.append("premium_after_discount = ").append(premiumAfterDiscount).append(",\n");
        }
        if (StringUtils.isNotBlank(netPremium)) {
            sb.append("net_premium = ").append(netPremium).append(",\n");
        }
        if (StringUtils.isNotBlank(carroDiscount)) {
            sb.append("premium_discount = ").append(carroDiscount).append(",\n");
        }
        if (StringUtils.isNotBlank(policyNo)) {
            sb.append("policy_no = '").append(policyNo).append("',\n");
            sb.append("insurer_policy_no = '").append(policyNo).append("',\n");
        }
        if (StringUtils.isNotBlank(insurerBasicPremium)) {
            sb.append("insurer_basic_premium = ").append(insurerBasicPremium).append(",\n");
        }
        if (StringUtils.isNotBlank(premiumDiscount)) {
            sb.append("insurer_premium_discount = ").append(premiumDiscount).append(",\n");
            sb.append("insurer_settlement_net_premium = ").append(netPremium).append(",\n");
        }
        if (StringUtils.isNotBlank(premiumDiscountPercent)) {
            sb.append("insurer_premium_discount_percent = ").append(premiumDiscountPercent).append(",\n");
        }
        if (StringUtils.isNotBlank(effectiveDate)) {
            sb.append("effective_date = '").append(effectiveDate).append("',\n");
        }
        if (StringUtils.isNotBlank(expiryDate)) {
            sb.append("expiry_date = '").append(expiryDate).append("',\n");
        }

        sb.append("status = 10\n");
        sb.append("where id = ").append(applicationId).append(";");

        return sb.toString();
    }

    private String updateTax(String applicationId,
                             String premiumTax,
                             String netPremium,
                             String carroDiscount) {
        return "update carro_trade_00.application_tax set \n" +
               "premium_tax = " + premiumTax + ",\n" +
               "premium_without_tax = " + netPremium + ",\n" +
               "premium_without_tax_discount = " + carroDiscount + "\n" +
               "where application_id = " + applicationId + ";";
    }

}
